import { SfwButton } from '@sfkj/sfw-dc-ui'; // 假设SfwQRCode是用于显示二维码的组件
import { Modal, ModalProps } from 'antd';
import React, { useState } from 'react';
import './WeChatBind.js';

// 定义SfwWeChatBind组件的属性类型
export type SfwWeChatBindProps = {
  onBindSuccess?: () => void; // 绑定成功后的回调
  onBindFail?: (error: any) => void; // 绑定失败后的回调
  getQRCodeUrl?: () => Promise<string>; // 获取二维码URL的异步函数
  userId: string;
} & ModalProps;

const SfwWeChatBind: React.FC<SfwWeChatBindProps> = (props) => {
  const { userId, ...restProps } = props;
  const [isModalOpen, setIsModalOpen] = useState(false);

  // 处理获取二维码URL的逻辑
  const weChatBind = () => {
    setIsModalOpen(true);
  };

  // 处理扫码成功后的逻辑（需要后端支持，此处仅为示例）
  // const handleScanSuccess = () => {
  //     onBindSuccess?.(); // 绑定成功，调用成功回调
  // };

  // 弹框打开后，生成二维码
  const DDNS_resolution = encodeURIComponent(
    `https://cscf-login.tgsc81.com/api/public/bind/callback?userId=${userId}`,
  );
  const modalChange = async (values: any) => {
    console.log(values);
    if (values) {
      let obj = new WxLogin({
        self_redirect: false,
        id: 'login_container',
        appid: 'wxc4a6580d547b1908',
        scope: 'snsapi_login',
        state: Math.random(),
        style: 'white',
        redirect_uri: DDNS_resolution,
      });
      console.log('obj', obj);
    }
  };
  const handleCancel = () => {
    setIsModalOpen(false);
  };
  // 渲染二维码和按钮等UI元素
  return (
    <div>
      <SfwButton type={'link'} size="small" onClick={weChatBind}>
        绑定微信
      </SfwButton>
      <Modal
        title="扫码绑定微信号"
        afterOpenChange={modalChange} // 打开或关闭时触发
        open={isModalOpen}
        onCancel={handleCancel}
        footer={null} // 自定义底部内容，不显示默认的确定和取消按钮
        {...restProps}
      >
        <>
          <div style={{ textAlign: 'center' }}>
            <div id="login_container"></div>
            <div>使用微信扫一扫登录</div>
            <div>煤链荣官方平台</div>
          </div>
        </>
      </Modal>
    </div>
  );
};

export default SfwWeChatBind;
